package customer.zspt.cn.customer.Widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import customer.zspt.cn.customer.R;

public class ContactLetterRightView extends LinearLayout {
    private Context mContext;
    private CharacterClickListener mListener;
    private List<String> letter = new ArrayList<>();
    private String Indexletter = "A";
    //每个textView的高度
    private int height=0;
    public ContactLetterRightView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setOrientation(VERTICAL);
        initView();
    }
    private void initView() {
        this.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        addView(buildImageLayout());
        letter.add("!");
        for (char i = 'A'; i <= 'Z'; i++) {
            final String character = i + "";
            TextView tv = buildTextLayout(character);
            letter.add(character);
            addView(tv);
        }
        addView(buildTextLayout("#"));
        letter.add("#");
    }
    private TextView buildTextLayout(final String character) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
        TextView tv = new TextView(mContext);
        tv.setTextSize(10);
        tv.setLayoutParams(layoutParams);
        tv.setGravity(Gravity.CENTER);
        tv.setClickable(true);
        tv.setText(character);
        tv.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        if (mListener != null) {
                            mListener.clickCharacter(character.charAt(0));
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        ContactLetterRightView.this.setBackgroundColor(Color.GRAY);
                        if (mListener != null) {
                            if(Indexletter.equals("!"))
                            {
                                mListener.clickArrow();
                                mListener.GoneView();
                            }
                            else
                            {
                                mListener.clickCharacter(Indexletter.charAt(0));
                            }

                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if (mListener != null) {
                            mListener.GoneView();
                        }
                        break;
                }
                return false;
            }
        });
        return tv;
    }
    private ImageView buildImageLayout() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
        ImageView iv = new ImageView(mContext);
        iv.setLayoutParams(layoutParams);
        iv.setBackgroundResource(R.drawable.jt);
        iv.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.clickArrow();
                }
            }
        });
        return iv;
    }
    public void setCharacterListener(CharacterClickListener listener) {
        mListener = listener;
    }
    public interface CharacterClickListener {
        void clickCharacter(char character);
        //回到顶部
        void clickArrow();
        //使显示索引的控件消失
        void GoneView();
    }
    //获得字母索引的高度
    public void setHeight(int height)
    {
        this.height = height;
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true);
        int y = 0;
        switch (ev.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                this.setBackgroundResource(R.color.serch_bg);
                y = (int)ev.getY();
                Indexletter = letter.get(y/height)+"";
                break;
            case MotionEvent.ACTION_MOVE:
                this.setBackgroundResource(R.color.serch_bg);
                y = (int) ev.getY();
                if(y<height*28&&y>0)
                {
                    Indexletter = letter.get(y/height)+"";
                }
            case MotionEvent.ACTION_UP:
                this.setBackgroundColor(Color.TRANSPARENT);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

}
